#!/usr/bin/perl -w

#===============================================================
# rcs-extract
# File ID: 26d14f32-5d43-11df-8375-90e6ba3022ac
# Extracts the last checkin from RCS files.
# Reads file names of the ,v files from stdin and stores the
# extracted data under Extracted/ with the same file structure
# it’s found. Example:
#   find -name '*,v' -type f | rcs-extract
#
# Character set: UTF-8
# License: GNU General Public License version 2 or later
# ©opyleft 2004 Øyvind A. Holm <sunny@sunbase.org>
#===============================================================

use strict;
use File::Path;

$| = 1;

my $Pref = ".Extracted/";

while (<>) {
	chomp;
	my $File = $_;
	my $end_found = 0;
	print("Opening $File...");
	if (open(FromFP, "<$File")) {
		binmode(FromFP);
		my $Dir = "";
		my $dest_dir = "$Pref";
		if ($File =~ m/^(.*)\//) {
			$Dir = $1;
			$dest_dir = "$Pref$Dir";
			D("[\$dest_dir = \"$dest_dir\"]");
			# -d $dest_dir || mkpath($dest_dir, 1) || warn("$dest_dir: Couldn’t create path: $!\n");
		}
		mkpath($dest_dir, 1);
		while (<>) {
			last if /^text$/;
		}
		$Pref =~ s#//#/#g;
		my $to_file = "$Pref$File";
		$to_file =~ s/^(.*),v$/$1/;
		if (-e $to_file) {
			my $Rand = ".rand.$$." . substr(random(), 0, 5);
			warn("$to_file: File already exists, adding random extension: $Rand");
			$to_file .= $Rand;
		}
		if (open(ToFP, ">$to_file")) {
			binmode(ToFP);
			my $Line = "";
			while (<FromFP>) {
				if (s/([^@])(@)([^@])/$1$3/g) {
					print(STDERR "[Found end]");
					$end_found = 1;
				}
				s/@@/@/g;
				$end_found && s/^\@$//;
				print(ToFP $_);
				$end_found && last;
			}
			close(ToFP);
			close(FromFP);
		} else {
			warn("$to_file: Unable to open file for write: $!");
		}
	} else {
		warn("$File: Unable to open file for read: $!\n");
	}
}

sub D {
	print(STDERR @_);
}

__END__

=pod

=head1 LICENCE

This program is free software; you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation; either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program; if not, write to the Free Software Foundation, Inc., 
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=cut

# vim: set fileencoding=UTF-8 filetype=perl foldmethod=marker foldlevel=0 :
# End of file rcs-extract
